import { useWebConfigStore, useUserStore, useRefs } from "@/stores";
// #ifdef MP-WEIXIN
import system from './mp-weixin/system';
// #endif
// #ifdef MP-TOUTIAO
import system from './mp-toutiao/system';
// #endif
const buildOptions = (options : any) => {
	// #ifdef H5
	const appid = system.appid();
	if (options.url.indexOf('?') > -1) {
		options.url += '&';
	} else {
		options.url += '?';
	}
	options.url += `appid=${appid}`;
	// #endif
	return options;
}
export const $page = {
	open: (params : UniApp.NavigateToOptions | string) => {
		let options : UniApp.NavigateToOptions = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		uni.navigateTo(buildOptions(options));
	},
	redirect: (params : UniApp.RedirectToOptions | string) => {
		let options : UniApp.RedirectToOptions = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		uni.redirectTo(buildOptions(options))
	},
	re: (params : UniApp.ReLaunchOptions | string) => {
		let options : UniApp.ReLaunchOptions = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		uni.reLaunch(buildOptions(options))
	},
	switch: (params : UniApp.SwitchTabOptions | string) => {
		let options : UniApp.SwitchTabOptions = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		uni.switchTab(buildOptions(options));
	},
	title: (title ?: string, subtitle ?: string) => {
		const webConfigStore = useWebConfigStore();
		const { WEBCONFIG } = useRefs(webConfigStore);
		const fastTitle = title || WEBCONFIG.value?.web_name || system.name();
		const lastTitle = subtitle !== undefined ? subtitle : '';
		uni.setNavigationBarTitle({
			title: fastTitle + lastTitle
		})
	},
	ads: (item : any) => {
		switch (item.page) {
			case "ARTICLE":
				$page.open(`/pages/article/content?alias=${item.url}`);
				break;
			case "SWITCH":
				$page.switch(item.url);
				break;
			case "CUSTOM":
				// #ifdef H5
				globalThis.open(item.url);
				// #endif
				break;
		}
	},
	shareMessage(options : any) {
		const webConfigStore = useWebConfigStore();
		const { WEBCONFIG } = useRefs(webConfigStore);
		const userStore = useUserStore();
		let obj : any = {
			...options
		};
		obj.title = WEBCONFIG.value?.share_message_title || options.title || system.name();
		obj.imageUrl = WEBCONFIG.value?.share_message_image || options.imageUrl || '';
		if (userStore.hasLogin()) {
			const { USERINFO } = useRefs(userStore);
			if (obj.path.indexOf('?') > -1) {
				obj.path += '&';
			} else {
				obj.path += '?';
			}
			obj.path += `puid=${USERINFO.value.id}`;
		}
		return obj;
	},
	shareTimeline(options : any) {
		const webConfigStore = useWebConfigStore();
		const { WEBCONFIG } = useRefs(webConfigStore);
		const userStore = useUserStore();
		let obj : any = {
			...options
		};
		obj.title = WEBCONFIG.value?.share_timeline_title || options.title || system.name();
		obj.imageUrl = WEBCONFIG.value?.share_timeline_image || options.imageUrl || '';
		if (userStore.hasLogin()) {
			const { USERINFO } = useRefs(userStore);
			if (obj.path.indexOf('?') > -1) {
				obj.path += '&';
			} else {
				obj.path += '?';
			}
			obj.path += `puid=${USERINFO.value.id}`;
		}
		return obj;
	},
}
export const login = (data : any, xLoginRef : any) => {
	const webConfigStore = useWebConfigStore();
	const { WEBCONFIG } = useRefs(webConfigStore);
	xLoginRef.value.show();
}